var url = "http://" + buildURL() + "/";
var commentCount=0;

$(function() {
	$('#droppable').html($("#content").val());
	
	
	var tblName = $("#tblName").val();
	var id = $("#requestID").val();
	$('#droppable').css('min-width','950px');
	
	if($(window).width()<1224){
		$('#rNav').hide();
		$('#droppable').css('width','950');
	}else{
		$('#rNav').show();
		$('#droppable').css('width',$(window).width()-300);
		$('#divLog').css('height',$(window).height()-400);	
	}
	
	$(window).resize(function(){
		if($(window).width()<1224){
			$('#rNav').hide();
		}else{
			$('#rNav').show();
			$('#droppable').css('width',$(window).width()-300);
			$('#divLog').css('height',$(window).height()-400);	
		}
	});
	
	if(id==0){
		var trackNo = CreateTrackNumber("PR","Stephen Curry")
		$("#TrackNo").val(trackNo);
		$("#tmpTrackNo").text(trackNo);
		
	}else{
		getDetails(tblName, id);
	}
	
	$("#commentPost").click(function(){
		var comment = $("#txtComment").val();
		addComment(tblName, id, comment)
	});
	
	
});

function getDetails(tblName, id){
	$.ajax({
		url: url + "loadrecord/" + tblName + "/" + id +"",
		success: function(xml){
			$(xml).find('request').each(function(){
				$(this).children().each(function(){
					var tagName=this.tagName;
					var val=$(this).text();
					setDetails(tagName,val);	
				});
			});
			addComment(tblName, id);
		}
	});
}


function setDetails(fieldName, fieldValue){

	var elementType =  String($('[name="' + fieldName + '"]').attr('type'));
	if(elementType=="checkbox" || elementType=="radio" ){
		var options = $('[name="' + fieldName + '"]');
		for(var i=0;i<options.length;i++){
			if(fieldValue.indexOf(options[i].value)>-1){
				$(options[i]).prop('checked', true);
			}
		}
	}else{
		$('[name="' + fieldName + '"]').val(fieldValue);
	}
}

function CreateTrackNumber(type,username)
{
	var d = new Date();

	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();
	var curr_sec = d.getSeconds();
	var curr_milsec = d.getMilliseconds();
	var curr_month = d.getMonth() + 1;
	var curr_day = d.getDate();
	var curr_year = d.getYear();
	
	var str = username;	
	var str_split = str.split(" ");
	var fname = Left(str_split[0].toUpperCase(),1);
	var lname;

	if (str_split.length > 1)
		lname = Left(str_split[str_split.length-1].toUpperCase(),1);
	else
		lname = Right(str_split[0].toUpperCase(),1);
	var str_month = String(curr_month)
	var str_day = String(curr_day)
	if (str_month.length < 2) 
		str_month = '0' + str_month
	if (str_day.length < 2)
		str_day = '0' + str_day
		
	var trackno = String(fname) + String(lname) + type  + str_month +str_day  + String(curr_year) +
	String(curr_hour) + String(curr_min) + String(curr_sec) + String(curr_milsec);
	
	return trackno;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function saveForm(lastAction){
	$("#Unread").val("1");
	
	document.frmForm.submit();
}


function addComment(tblName, id, comment){
	$.ajax({
		url: url + "comment/" + tblName + "/" + id + "/" +  comment,
		cache: false
		}).done(function( html ) {
			$("#txtComment").val("");
			$("#divComment").html("");
			$("#divComment").append(html);
			
			$("#divComment").jScrollPane({
				horizontalGutter:5,
				verticalGutter:5,
				'showArrows': false,
				autoReinitialise: true
			});
			
			$('.jspDrag').hide();
			$('.jspScrollable').mouseenter(function(){
				$(this).find('.jspDrag').stop(true, true).fadeIn('slow');
			});
				
			$('.jspScrollable').mouseleave(function(){
				$(this).find('.jspDrag').stop(true, true).fadeOut('slow');
		});
	});
}
